/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

extern crate alloc;

use alloc::boxed::Box;
use crate::Vec;

use crate::commands::auth_commands::AuthParam;
use crate::commands::common_command::{AuthResultInfo, ScheduleInfo};
use crate::commands::enrollment_commands::{EnrollParam, EnrollResultInfo};
use crate::common::constants::ErrorCode;
use crate::log_e;
use crate::singleton_registry;
use crate::utils::AuthToken;

pub trait AuthContext {
    fn schedule_id(&self) -> u64;
    fn context_id(&self) -> u64;
    fn begin(&mut self, auth_param: &AuthParam) -> Result<Vec<ScheduleInfo>, ErrorCode>;
    fn update_result(&mut self, schedule_result: &Vec<u8>) -> Result<AuthResultInfo, ErrorCode>;
}

pub trait EnrollContext {
    fn schedule_id(&self) -> u64;
    fn begin(
        &mut self,
        auth_token: &AuthToken,
        enroll_param: &EnrollParam,
    ) -> Result<Vec<ScheduleInfo>, ErrorCode>;
    fn update_result(
        &mut self,
        user_id: i32,
        schedule_result: &Vec<u8>,
    ) -> Result<EnrollResultInfo, ErrorCode>;
}

pub trait ContextManager {
    fn add_auth_context(&mut self, context: Box<dyn AuthContext>) -> Result<(), ErrorCode>;
    fn get_auth_context<'a>(&'a self, context_id: u64) -> Result<&'a dyn AuthContext, ErrorCode>;
    fn get_auth_context_by_schedule_id<'a>(&'a self, schedule_id: u64) -> Result<&'a dyn AuthContext, ErrorCode>;
    fn remove_auth_context(&mut self, context_id: u64) -> Result<(), ErrorCode>;

    fn add_enroll_context(&mut self, context: Box<dyn EnrollContext>) -> Result<(), ErrorCode>;
    fn get_enroll_context<'a>(&'a self) -> Result<&'a dyn EnrollContext, ErrorCode>;
    fn remove_enroll_context(&mut self) -> Result<(), ErrorCode>;
    fn generate_unique_schedule_id(&self) -> Result<u64, ErrorCode>;
    fn check_context_id_duplicate(&self, context_id: u64) -> Result<bool, ErrorCode>;
}

pub struct DummyContextManager;

impl ContextManager for DummyContextManager {
    fn add_auth_context(&mut self, _context: Box<dyn AuthContext>) -> Result<(), ErrorCode> {
        log_e!("not implemented");
        Err(ErrorCode::GeneralError)
    }
    fn get_auth_context<'a>(&'a self, _context_id: u64) -> Result<&'a dyn AuthContext, ErrorCode> {
        log_e!("not implemented");
        Err(ErrorCode::GeneralError)
    }
    fn get_auth_context_by_schedule_id<'a>(&'a self, _schedule_id: u64) -> Result<&'a dyn AuthContext, ErrorCode> {
        log_e!("not implemented");
        Err(ErrorCode::GeneralError)
    }
    fn remove_auth_context(&mut self, _context_id: u64) -> Result<(), ErrorCode> {
        log_e!("not implemented");
        Err(ErrorCode::GeneralError)
    }

    fn add_enroll_context(&mut self, _context: Box<dyn EnrollContext>) -> Result<(), ErrorCode> {
        log_e!("not implemented");
        Err(ErrorCode::GeneralError)
    }
    fn get_enroll_context<'a>(&'a self) -> Result<&'a dyn EnrollContext, ErrorCode> {
        log_e!("not implemented");
        Err(ErrorCode::GeneralError)
    }
    fn remove_enroll_context(&mut self) -> Result<(), ErrorCode> {
        log_e!("not implemented");
        Err(ErrorCode::GeneralError)
    }
    fn generate_unique_schedule_id(&self) -> Result<u64, ErrorCode> {
        log_e!("not implemented");
        Err(ErrorCode::GeneralError)
    }
    fn check_context_id_duplicate(&self, _context_id: u64) -> Result<bool, ErrorCode> {
        log_e!("not implemented");
        Err(ErrorCode::GeneralError)
    }
}

singleton_registry!(
    ContextManagerRegistry,
    ContextManager,
    DummyContextManager
);
